<?php
include '../login/db_connect.php';
include '../login/functions.php';
sec_session_start ();
if (isset ( $_COOKIE ['login_string'] )) {
	$login_string = $_COOKIE ['login_string'];
	$username = $_COOKIE ['username'];
	$user_id = $_COOKIE ['user_id'];
	
	$_SESSION ['user_id'] = $user_id;
	$_SESSION ['login_string'] = $login_string;
	$_SESSION ['username'] = $username;
}

if (login_check ( $mysqli ) == true) {
	$username = $_SESSION ['username'];
	global $ruolo;
	$query_ruolo = "SELECT ruolo, saldo FROM members WHERE username='" . $username . "' LIMIT 1";
	if ($result = $mysqli->query ( $query_ruolo )) {
		while ( $row = $result->fetch_assoc () ) {
			$ruolo = $row ['ruolo'];
			$saldo = $row ['saldo'];
		}
		$result->free ();
	}
	?>
<!DOCTYPE html>
<html lang="it">
<head>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>Gestione listini</title>

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="../js/bootstrap.min.js"></script>
<link href="../css/bootstrap.min.css" rel="stylesheet">


</head>
<body>
	<!-- 	barra di navigazione -->
	<nav class="navbar navbar-inverse navbar-static-top" role="navigation">
		<div class="container">
			<div class="navbar-header">
				<button type="button" class="navbar-toggle collapsed"
					data-toggle="collapse" data-target="#menu_links">
					<span class="sr-only">Toggle navigation</span> <span
						class="icon-bar"></span> <span class="icon-bar"></span> <span
						class="icon-bar"></span>
				</button>
				<a href="./home.php"><img src="../images/logo.png" height=45px
					width=45px></a>
			</div>
			<div class="collapse navbar-collapse" id=menu_links>
				<ul class="nav navbar-nav">
					<li><a href="./home.php"><span class="glyphicon glyphicon-home"
							aria-hidden="true"></span>&nbsp;Home</a></li>
					<li><a href="profile.php">Profile</a></li>
					<li><a href="messages">Messages</a></li>
				</ul>
				<p class="navbar-text navbar-right">
					Signed in as <a href="#" class="navbar-link"><mark><?php echo $username?></mark></a>&nbsp;&nbsp;&nbsp;<a
						href="../login/logout.php" class="navbar-link"><span
						class="glyphicon glyphicon-log-out" aria-hidden="true"></span>Logout</a>
				</p>
			</div>
		</div>
	</nav>

	<!-- inizio del contenitore della pagina -->
	<div class="container">




		<div class="row">
			<div class="col-sm-7">
				<div class="panel panel-primary">
					<div class="panel-heading">
						<h3 class="panel-title">Ricevuti</h3>
					</div>
					<div class="panel-body">
						<table class="table table-condensed table-striped"info'>
							<tr>
								<th align=center>MITTENTE
								
								<th align=center>OGGETTO
								
								<th align=center>DATA e ORA
								
								<td>Opzioni</td>
							</tr>
				
				
						<?php
	$query = "SELECT * FROM message";
	if ($result = $mysqli->query ( $query )) {
		/* fetch associative array */
		$count = 0;
		while ( $row = $result->fetch_assoc () ) {
			if ($row ['receiver'] == 19) {
				$count = $count + 1;
				
				echo "<tr>";
				echo "<td>", $row ['sender'];
				echo "<td>", $row ['subject'];
				echo "<td>", $row ['send_date'];
				$message = $row ['content'];
				
				echo "<td>", "<button onclick='$(this).popover();' type=\"button\" class=\"btn btn-link\" data-placement=\"left\" data-container=\"body\" data-toggle=\"popover\" data-content=\"$message\"><span class=\"glyphicon glyphicon-eye-open\"></span></button>";
				
				echo "</tr>";
			}
		}
		
		/* free result set */
		// $result->free ();
	}
	?>
						</table>
					</div>
				</div>

				<div class="panel panel-primary">
					<div class="panel-heading">
						<h3 class="panel-title">Inviati</h3>
					</div>
					<div class="panel-body">
						<table class="table table-condensed table-striped"info'>
							<tr>
								<th align=center>MITTENTE
								
								<th align=center>OGGETTO
								
								<th align=center>DATA e ORA
								
								<td>Opzioni</td>
							</tr>
				
				
						<?php
	$query = "SELECT * FROM message";
	if ($result = $mysqli->query ( $query )) {
		/* fetch associative array */
		$count = 0;
		while ( $row = $result->fetch_assoc () ) {
			if ($row ['sender'] == 19) {
				$count = $count + 1;
				$message = $row ['content'];
				echo "<tr>";
				echo "<td>", $row ['sender'];
				echo "<td>", $row ['subject'];
				echo "<td>", $row ['send_date'];
				echo "<td>", "<button onclick='$(this).popover();' type=\"button\" class=\"btn btn-link\" data-placement=\"left\" data-container=\"body\" data-toggle=\"popover\" data-content=\"$message\"><span class=\"glyphicon glyphicon-eye-open\"></span></button>";
				
				echo "</tr>";
			}
		}
		
		/* free result set */
		// $result->free ();
	}
	?></table>
					</div>
				</div>
			</div>

			<div class="col-sm-5">
				<div class="panel panel-success">
					<div class="panel-heading">
						<h3 class="panel-title">New Message</h3>
					</div>
					<div class="panel-body">
						<!-- FORM PER CREARE MESSAGGIO -->
						<form title="form_new_message" name=newmessform
							action="message_process.php" method=POST id="newmessage">
					
							<div class="form-group">
								<table>
									<tr>
										<td><label>Destinatario</label>
										
										<td><label>Mittente</label>
									
									
									<tr>
										<td><input type=text name=destinatario class=form-control required
											placeholder="Destinatario">
										
										<td><input type=text name=mittente class=form-control placeholder="<?php echo $username;?>" disabled="disabled"></label>
								
								</table>
								
							</div>

							<div class="form-group">
								<label>Oggetto</label><input type=text
									name=oggetto class=form-control required placeholder="Oggetto">
							</div>

							<div class="form-group">
								<label for="Messaggio">Messaggio</label>
								<textarea rows="8" name=messaggio class=form-control required
									placeholder="Messaggio"></textarea>
							</div>

							<button class="btn btn-lg btn-success btn-block" type="submit">Invia messaggio</button>
						</form>

					</div>
				</div>
			</div>


		</div>
	</div>

</body>
</html>

<?php
} else {
	header ( "Location: ../login/login.php" );
	// echo 'You are not authorized to access this page, please login. <br/>';
}
?>










